package com.my.screenadapter.net;

import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonObject;
import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.utils.PassWordUtil;
import com.my.screenadapter.R;
import com.my.screenadapter.net.retrofit.RetrofitUtil;
import com.my.screenadapter.net.volley.VolleyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * Created by ${张英杰} on 2017/5/14.
 */

public class HttpCompareActivity extends BaseActivity {

    private RequestQueue mQueue;
    private String upUrl="http://172.16.103.234:10000/api/userauth";
    private String afa="MC2mqoTg+zIERY\\/K+7xlsw==";
    private String sf="MC2mqoTg+zIERY/K+7xlsw==";

    @Override
    protected void init() {

    }

    @Override
    protected int initLayoutId() {
        mQueue = VolleyUtils.getRequestQueue();
        return R.layout.activity_httpcompare;
    }

    @OnClick(R.id.json)
    public void upJson(){
        JSONObject JJobj=new JSONObject();
        Map<String, String> ingredients = new HashMap<String, String>();
        ingredients.put("username", "123");
        try {
            JJobj.put("username", "123");
            JJobj.put("password", PassWordUtil.myEncrypt("456").trim());
        } catch (JSONException e) {
            e.printStackTrace();
        }



        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("username", "123");
        jsonObject.addProperty("password", PassWordUtil.myEncrypt("456").trim());

        VolleyUtils.postJSON(upUrl, jsonObject.toString(), new Response.Listener() {
            @Override
            public void onResponse(Object response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @OnClick(R.id.token)
    public void getDataByToken(){

        VolleyUtils.getTokenRequest(upUrl, "TTTTtoken", new Response.Listener() {
            @Override
            public void onResponse(Object response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RetrofitUtil.getRetrofitStr().getTokenData("RRRRRToken")
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, retrofit2.Response<String> response) {
                        Log.i("=====",response+"");
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                      t.printStackTrace();
                    }
                });
    }

    //up TOken and Json
    @OnClick(R.id.header)
    public void upDataByToken(){
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("username", "userAAA");
        jsonObject.addProperty("password", "pppBBB");

//        VolleyUtils.getTokenRequest(upUrl, "TTTTtoken", new Response.Listener() {
//            @Override
//            public void onResponse(Object response) {
//
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });

//        Map<String, RequestBody> bodymap = new HashMap<>();
//        bodymap.put(ImageSelectCache.Type_Video + (TextUtils.isEmpty(imageSelectCache.getFPhotoContent()) ? "" : imageSelectCache.getFPhotoContent()) + "\";filename=\"" + file.getName(),
//        RequestBody.create(MediaType.parse("multipart/form-lifecircle"), file));
        RetrofitUtil.getRetrofitStr().upTokenData("RRRRRToken",
                RequestBody.create(MediaType.parse("text/json"), jsonObject.toString()))
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, retrofit2.Response<String> response) {
                        Log.i("=====",response+"");
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                      t.printStackTrace();
                    }
                });
    }

    @BindView(R.id.et_content)
    EditText etContent;
    private static final String TAG = "HttpCompareActivity";
    public void startEncode(View view) {

        Log.i(TAG, "startEncode: ===="+PassWordUtil.myEncrypt(etContent.getText().toString().trim()));
    }
}
